package com.gis3c.sys.security.shiro.filter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.web.filter.AccessControlFilter;
import org.apache.shiro.web.util.SavedRequest;
import org.apache.shiro.web.util.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;

public class C3FormAuthenticationFilter extends FormAuthenticationFilter {
	private static final Logger log = LoggerFactory.getLogger(FormAuthenticationFilter.class);
	
	public C3FormAuthenticationFilter(){
		super();
	}
	
	@Override
	protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
		if (isLoginRequest(request, response)) {
            if (isLoginSubmission(request, response)) {
                if (log.isTraceEnabled()) {
                    log.trace("Login submission detected.  Attempting to execute login.");
                }

                return executeLogin(request, response);
            } else {
//                if (log.isTraceEnabled()) {
//                    log.trace("Login page view.");
//                }

                return true;
            }
        } else {
            if (log.isTraceEnabled()) {
                log.trace("Attempting to access a path which requires authentication.  Forwarding to the " +
                        "Authentication url [" + getLoginUrl() + "]");
            }

            String header = ((HttpServletRequest)request).getHeader("X-Requested-With");
            if(header != null && !"".equals(header)
            		&&(header.equals("X-Requested-With") || header.equals("XMLHttpRequest"))){
            	response.setContentType("application/json;charset=UTF-8");
            	HttpServletResponse httpResponse = (HttpServletResponse) response;
        		httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        		httpResponse.getWriter().write("You do not have access rights!");
            }else{
//                saveRequestAndRedirectToLogin(request, response);

                HttpServletRequest req = (HttpServletRequest) request;

                saveRequestAndRedirectToLogin(request, response);

//                if(!"/href=".equals(req.getRequestURI().toString())){
//                    saveRequestAndRedirectToLogin(request, response);
//                }
//                else {
//                    SavedRequest savedRequest = WebUtils.getAndClearSavedRequest(request);
//                    boolean contextRelative = true;
//                    String successUrl = null;
//                    if(savedRequest != null && savedRequest.getMethod().equalsIgnoreCase(AccessControlFilter.GET_METHOD)){
//                        successUrl = savedRequest.getRequestUrl();
//                        contextRelative = false;
//                    }
//                    WebUtils.issueRedirect(request, response, successUrl, null,contextRelative);
//                }
            }
            return false;
        }
    }
}
